package com.paradise.phone.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.Window;
import android.view.WindowManager;

import com.paradise.api.FaceLogin;
import com.paradise.api.FacephonePreferences;
import com.paradise.phone.FacephoneService;
import com.paradise.phone.KeepLiveService;
import com.paradise.phone.R;
import com.paradise.phone.utils.DataBaseHelper;

/**
 * Created by Sisyphus on 2017/9/5.
 */

public class LaunchActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_launch);

        startService(new Intent(getApplicationContext(), FacephoneService.class));
        startService(new Intent(getApplicationContext(), KeepLiveService.class));

        SharedPreferences sp = this.getSharedPreferences("face_phone", MODE_PRIVATE);

        if (sp.getString("first_launch", "TRUE").equals("TRUE")) {

            FacephonePreferences.instance().setAutomaticallyAcceptVideoRequests(true);
            FacephonePreferences.instance().enableAutoAnswer(true);
            FacephonePreferences.instance().setInitiateVideoCall(true);

            SharedPreferences.Editor editor = sp.edit();
            editor.putString("first_launch", "FALSE");
            editor.commit();

            DataBaseHelper dbHelper = DataBaseHelper.instance(this);
            dbHelper.getReadableDatabase();
        }

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (FaceLogin.instance().getCurrentAuthInfo() == null) {
                    startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                }else {
                    startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                }
                finish();
            }
        }, 1000);
    }
}
